import axios from 'axios'
import { Message } from 'element-ui';
import { getQueryString, isNotEmptyObject, getStorage ,makeSign,encrypt} from "UTILS/util";

let baseURL =  process.env.VUE_APP_BASE_API
if(window.location.href.indexOf("https://")>-1){
  baseURL = process.env.VUE_APP_BASE_API_HTTPS;
}
// create an axios instance
const service = axios.create({
  baseURL: baseURL, // api的base_url
  timeout: 120000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // Do something before request is sent
    // if (store.getters.token) {
    // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
    // config.headers['X-Token'] = store.getters.token;
    // }

    //统一URL 以后如果打包，这个api要去掉 写到baseURL里面去22222
    if(!config.url) {
      config.url = "router";
    }
    if(process.env.VUE_APP_build_type == 'development') {
      config.baseURL ="";
      //config.url = "http://10.1.6.124:40017/router"
      config.url ="http://192.168.36.180:40000/router";
      
      // config.url ="/cgb/router";
      // config.url ="/hezhi/router";

      // config.url ="/hezhi/router";


      // config.url ="/zly/router";

      // // config.url ="http://192.168.37.6:40000/router";


    }
    // config.url ="/dragon/router";
    // 统一添加req信息

    let nonce = Math.random().toString().slice(-6);
    let timestamp =  new Date().getTime();
    let appid = 'lNJ0OT44hxSvpd7Y';
  

    let userInfo = getStorage("cst_userInfo");
    userInfo = userInfo?JSON.parse(userInfo):{};
    config.data.req = {
      appid:appid,
      nonce: nonce,
      timestamp: timestamp,
      sign: makeSign(appid,nonce,timestamp),
      YHID: userInfo.YHID || null,
      token: userInfo.token || null,
    }
   
    return config;
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// respone interceptor
service.interceptors.response.use(
  response => {
    let res = response.data;
    /**
     * 有时候需要针对性进行特殊提示，就不需要统一的提示，直接在request里面加
     * 入参 returnAllCodeResponse: true即可
     */
    if(response.config.returnAllCodeResponse) {
      return Promise.resolve(res);
    }

    if(res.code === 0) {
      return Promise.resolve(res);
    }else if(res.code == -105){
      window.localStorage.clear();
      // location.reload()// In order to re-instantiate the vue-router object to avoid bugs
      // console.log(window.location)
      window.location.href =window.location.href.split("#")[0]+'#/login';
    }else{
      let msg = res.msg;
      if(typeof msg == 'object'){
        msg = JSON.stringify(msg)
      }
      Message({
        showClose: true,
        message: msg || '功能异常，请联系管理员',
        type: 'error',
        duration: 3 * 1000
      })
    
      return Promise.reject(res);
    }
  },
  error => {
    console.log("error"+ error)
    let info = {
      duration: 6000,
      message: '',
      type: 'error',
    };
    if(!error.response) { //接口请求超时
      Message({
        ...info,
        message: "后台响应超时，请刷新页面重新请求，或联系技术人员"
      });
    }else{
      Message({
        ...info,
        message: "系统功能异常，请刷新页面重新请求，或联系技术人员"
      });
    }
    return Promise.reject(error);
  }
)

export default service
